/*
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dianping.zebra.filter;

import java.beans.PropertyChangeEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import com.dianping.zebra.group.config.datasource.entity.DataSourceConfig;
import com.dianping.zebra.group.datasources.FailOverDataSource;
import com.dianping.zebra.group.jdbc.GroupConnection;
import com.dianping.zebra.group.jdbc.GroupDataSource;
import com.dianping.zebra.shard.jdbc.ShardDataSource;
import com.dianping.zebra.shard.jdbc.ShardResultSet;
import com.dianping.zebra.shard.jdbc.ShardStatement;
import com.dianping.zebra.shard.router.RouterResult;
import com.dianping.zebra.single.jdbc.SingleConnection;
import com.dianping.zebra.single.jdbc.SingleDataSource;
import com.dianping.zebra.single.jdbc.SingleResultSet;
import com.dianping.zebra.single.jdbc.SingleStatement;

public interface JdbcFilter {
	int DEFAULT_ORDER = 0;

	int MAX_ORDER = Integer.MAX_VALUE;

	int MIN_ORDER = Integer.MIN_VALUE;

	/**
	 * filter ordering <br>
	 * filter_with_order_3_start filter_with_order_2_start
	 * filter_with_order_1_start targer_start filter_with_order_1_finish
	 * filter_with_order_2_finish filter_with_order_3_finish
	 *
	 * @return the order of execute
	 */
	int getOrder();

	/**
	 * init filter
	 */
	void init();

	/** GroupDataSource Filter **/
	void initGroupDataSource(GroupDataSource source, JdbcFilter chain);

	void refreshGroupDataSource(GroupDataSource source, String propertiesName, JdbcFilter chain);

	GroupConnection getGroupConnection(GroupDataSource source, JdbcFilter chain) throws SQLException;

	FailOverDataSource.FindMasterDataSourceResult findMasterFailOverDataSource(
			FailOverDataSource.MasterDataSourceMonitor source, JdbcFilter chain);

	void closeGroupConnection(GroupConnection source, JdbcFilter chain) throws SQLException;

	void closeGroupDataSource(GroupDataSource source, JdbcFilter chain) throws SQLException;

	void switchFailOverDataSource(FailOverDataSource source, JdbcFilter chain);

	/** SingleDataSource Filter **/
	DataSource initSingleDataSource(SingleDataSource source, JdbcFilter chain);

	SingleConnection getSingleConnection(SingleDataSource source, JdbcFilter chain) throws SQLException;

	String processSQL(DataSourceConfig dsConfig, SQLProcessContext ctx, JdbcFilter chain) throws SQLException;

	<T> T executeSingleStatement(SingleStatement source, SingleConnection conn, String sql, List<String> batchedSql,
			boolean isBatched, boolean autoCommit, Object params, JdbcFilter chain) throws SQLException;

	void closeSingleConnection(SingleConnection source, JdbcFilter chain) throws SQLException;

	void closeSingleDataSource(SingleDataSource source, JdbcFilter chain) throws SQLException;

	void closeSingleResultSet(SingleResultSet source, JdbcFilter chain) throws SQLException;

	/** ShardDataSource Filter **/

	void initShardDataSource(ShardDataSource source, JdbcFilter chain);

	ResultSet executeShardQuery(ShardStatement source, String sql, JdbcFilter chain) throws SQLException;

	int executeShardUpdate(ShardStatement source, String sql, int autoGeneratedKeys, int[] columnIndexes,
			String[] columnNames, JdbcFilter chain) throws SQLException;

	void shardRouting(RouterResult rr, JdbcFilter chain) throws SQLException;

	void shardMerge(ShardResultSet rs, JdbcFilter chain) throws SQLException;

	void configChanged(PropertyChangeEvent evt, JdbcFilter chain);
}
